import 'dart:async';

import 'package:flutter/material.dart';

import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:chewie/chewie.dart';
import 'package:chewie/src/chewie_player.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter/cupertino.dart';

// 横屏和竖屏
import 'package:flutter/services.dart';
import 'package:orientation/orientation.dart';

class videoPreview extends StatefulWidget {
  @override
  _VideoAppState createState() => _VideoAppState();
}

class _VideoAppState extends State<videoPreview> {
  var currentVideoData = new Map();
  var ssd = 'http://vod.bettermorph.cn/f2c9f425184044b882fa0bd698a99351/1a2a32337edb41a9a9055692c7bdae5c-a15b64ab528b72338e1251b9df372f58-ld.mp4';

  TargetPlatform _platform;
  VideoPlayerController _videoPlayerController1;
  ChewieController _chewieController;

  /**
   * 获取存在SharedPreferences中的数据
   */
  Future getString() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String temCurrentVideoData = sharedPreferences.getString('current_video');
    Map<String, dynamic> temObjData = json.decode(temCurrentVideoData);
    setState(() {
      currentVideoData = temObjData;
      ssd = temObjData['videoUrl'];
      // currentVideoUrl = temObjData['videoUrl'];
    });
    print('ssd - getString');
    print(ssd);

  }

  @override
  void initState() {
    super.initState();

    getString();

    print('ssd - initState');
    print(ssd);

    _videoPlayerController1 = VideoPlayerController.network(
        ssd);
    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController1,
      aspectRatio: 3 / 2,
      autoPlay: true,
      looping: false,
      // Try playing around with some of these other options:

      //showControls: false,
      materialProgressColors: ChewieProgressColors(
        playedColor: Colors.red,
        handleColor: Colors.blue,
        backgroundColor: Colors.grey,
        bufferedColor: Colors.lightGreen,
      ),
      placeholder: Container(
        color: Colors.white,
      ),
      autoInitialize: true,
    );

    OrientationPlugin.forceOrientation(DeviceOrientation.landscapeLeft);

    SystemChrome.setPreferredOrientations(
        [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
  }

  @override
  void didChangeDependencies() {
    print('didChangeDependencies');
    super.didChangeDependencies();
    print('ssd - didChangeDependencies');
    print(ssd);
  }

  @override
  void dispose() {
    // Ensure disposing of the VideoPlayerController to free up resources.
    super.dispose();

    _videoPlayerController1.dispose();
    _chewieController.dispose();

    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
    OrientationPlugin.forceOrientation(DeviceOrientation.portraitUp);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Butterfly Video'),
      ),
      // Use a FutureBuilder to display a loading spinner while waiting for the
      // VideoPlayerController to finish initializing.
      body: Column(
        children: <Widget>[
          Expanded(
            child: Center(
              child: Chewie(
                controller: _chewieController,
              ),
            ),
          ),
//          FlatButton(
//            onPressed: () {
//              _chewieController.enterFullScreen();
//            },
//            child: Text('Fullscreen'),
//          ),
//          Row(
//            children: <Widget>[
//              Expanded(
//                child: FlatButton(
//                  onPressed: () {
//                    setState(() {
//                      _chewieController.dispose();
//                      _chewieController = ChewieController(
//                        videoPlayerController: _videoPlayerController1,
//                        aspectRatio: 3 / 2,
//                        autoPlay: true,
//                        looping: true,
//                      );
//                    });
//                  },
//                  child: Padding(
//                    child: Text("Video 1"),
//                    padding: EdgeInsets.symmetric(vertical: 16.0),
//                  ),
//                ),
//              ),
//            ],
//          ),
        ],
      ),
    );
  }
}
